package com.rtwo.beauty;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.orhanobut.logger.Logger;

/**
 * rtwo 创建于 2016/4/11 15:45
 */
public class WelcomeActivity extends Activity implements View.OnClickListener{

    private EditText password;
    private Button enter;
    private Button cancle;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        password = (EditText)findViewById(R.id.password);
        enter = (Button)findViewById(R.id.enter);
        cancle = (Button)findViewById(R.id.cancle);
        enter.setOnClickListener(this);
        cancle.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.enter:
                String pass = password.getText().toString();
                savePassword(pass);
                break;
            case R.id.cancle:
                this.finish();
                break;
            default:
                break;
        }
    }

    public void savePassword(String pass){
        SharedPreferences sp = getSharedPreferences("password", MODE_PRIVATE);
        if(pass.isEmpty()){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        String pass1 = sp.getString("pass", "");
        Logger.d("原始密码为"+pass1);
        if(pass1.equals("")){
            SharedPreferences.Editor edit = sp.edit();
            edit.putString("pass",pass);
            edit.commit();
            Intent intent = new Intent(this,SettingActivity.class);
            startActivity(intent);
            this.finish();
        }else{
            Logger.d("输入密码为"+pass);
            if(pass.equals(pass1)){
                Logger.v("密码正确");
                Intent intent = new Intent(this,SettingActivity.class);
                startActivity(intent);
                this.finish();
            }
            else{
                Toast.makeText(this,"密码不正确",Toast.LENGTH_SHORT).show();
            }
        }
    }
}
